- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path478. Generate Random Point in a Circle.go
46 lines (40 loc) · 899 Bytes
/
478. Generate Random Point in a Circle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package leetcode
import (
"math"
"math/rand"
"time"
)
typeSolutionstruct {
rfloat64
xfloat64
yfloat64
}
funcConstructor(radiusfloat64, x_centerfloat64, y_centerfloat64) Solution {
rand.Seed(time.Now().UnixNano())
returnSolution{radius, x_center, y_center}
}
func (this*Solution) RandPoint() []float64 {
/*
a := angle()
r := this.r * math.Sqrt(rand.Float64())
x := r * math.Cos(a) + this.x
y := r * math.Sin(a) + this.y
return []float64{x, y}*/
for {
rx:=2*rand.Float64() -1.0
ry:=2*rand.Float64() -1.0
x:=this.r*rx
y:=this.r*ry
ifx*x+y*y<=this.r*this.r {
return []float64{x+this.x, y+this.y}
}
}
}
funcangle() float64 {
returnrand.Float64() *2*math.Pi
}
/**
* Your Solution object will be instantiated and called as such:
* obj := Constructor(radius, x_center, y_center);
* param_1 := obj.RandPoint();
*/